package cn.com.dcsgo.common.validation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * InIfNotNullValidator
 *
 * @author Dcsgo
 * @since 28/5/2025 上午 10:21
 */

public class InIfNotNullValidator implements ConstraintValidator<InIfNotNull, Object> {
    private Set<String> valueSet;

    @Override
    public void initialize(InIfNotNull constraintAnnotation) {
        valueSet = new HashSet<>(Arrays.asList(constraintAnnotation.value()));
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        // null 直接通过
        if (value == null) {
            return true;
        }
        // 转为字符串后判断
        return valueSet.contains(value.toString());
    }
}
